using System;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace SalesVMT
{
    public class SpendDB
    {

        public static DataSet GetOne(int _Spend_ID, string _Spend_Type)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Spend_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Spend_ID", _Spend_ID);
            dbCmd.Parameters.AddWithValue("@Spend_Type", _Spend_Type);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public static DataSet GetList(out int ItemCount, string keyWord, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Spend_GetList", dbConn);

            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static bool Delete(int _Spend_ID)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Spend_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Spend_ID", _Spend_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(SpendInfo _SpendInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Spend_Insert", dbConn);
            dbCmd.Parameters.AddWithValue("@Spend_Code", _SpendInfo.Spend_Code);
            dbCmd.Parameters.AddWithValue("@Spend_Date", _SpendInfo.Spend_Date);
            dbCmd.Parameters.AddWithValue("@Spend_ProviderID", _SpendInfo.Spend_ProviderID);
            dbCmd.Parameters.AddWithValue("@Spend_Receive", _SpendInfo.Spend_Receive);
            dbCmd.Parameters.AddWithValue("@Spend_Reason", _SpendInfo.Spend_Reason);
            dbCmd.Parameters.AddWithValue("@Spend_Total", _SpendInfo.Spend_Total);
            dbCmd.Parameters.AddWithValue("@Spend_UsersID", _SpendInfo.Spend_UsersID);
            dbCmd.Parameters.AddWithValue("@Spend_Type", _SpendInfo.Spend_Type);

            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public static SpendInfo GetInfo(int _Spend_ID, string _Spend_Type)
        {
            SpendInfo retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Spend_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Spend_ID", _Spend_ID);
            dbCmd.Parameters.AddWithValue("@Spend_Type", _Spend_Type);
            dbCmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new SpendInfo();
                    retVal.Spend_ID = Convert.ToInt32(dr["Spend_ID"]);
                    retVal.Spend_Code = Convert.ToString(dr["Spend_Code"]);
                    retVal.Spend_Date = Convert.ToDateTime(dr["Spend_Date"]);
                    retVal.Spend_ProviderID = Convert.ToInt32(dr["Spend_ProviderID"]);
                    retVal.Spend_Receive = Convert.ToString(dr["Spend_Receive"]);
                    retVal.Spend_Reason = Convert.ToString(dr["Spend_Reason"]);
                    retVal.Spend_Total = Convert.ToDecimal(dr["Spend_Total"]);
                    retVal.Spend_UsersID = Convert.ToInt32(dr["Spend_UsersID"]);
                    retVal.Spend_Type = Convert.ToString(dr["Spend_Type"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public static string GetMaxCode()
        {
            string retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Spend_GetMaxCode", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToString(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
       

    }
}
